package com.craftplay.dao;

import com.craftplay.model.Offer;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface OfferMapper {
    @Delete({
        "delete from offer",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into offer (unit_id, ",
        "owner_id, actor_id, ",
        "offer_price, place_provided, ",
        "place_fee, tool, ",
        "tool_fee, time_len, ",
        "leave_word, status, ",
        "created_time)",
        "values ( #{unitId,jdbcType=INTEGER}, ",
        "#{ownerId,jdbcType=INTEGER}, #{actorId,jdbcType=INTEGER}, ",
        "#{offerPrice,jdbcType=INTEGER}, #{placeProvided,jdbcType=INTEGER}, ",
        "#{placeFee,jdbcType=VARCHAR}, #{tool,jdbcType=INTEGER}, ",
        "#{toolFee,jdbcType=VARCHAR}, #{timeLen,jdbcType=INTEGER}, ",
        "#{leaveWord,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, ",
        "#{createdTime,jdbcType=TIMESTAMP})"
    })
    int insert(Offer record);

    @InsertProvider(type=OfferSqlProvider.class, method="insertSelective")
    int insertSelective(Offer record);

    @Select({
        "select",
        "id, unit_id, owner_id, actor_id, offer_price, place_provided, place_fee, tool, ",
        "tool_fee, time_len, leave_word, status, created_time",
        "from offer",
        "where id = #{id,jdbcType=INTEGER} and status != -1 and status != 1"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
        @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
        @Result(column="actor_id", property="actorId", jdbcType=JdbcType.INTEGER),
        @Result(column="offer_price", property="offerPrice", jdbcType=JdbcType.INTEGER),
        @Result(column="place_provided", property="placeProvided", jdbcType=JdbcType.INTEGER),
        @Result(column="place_fee", property="placeFee", jdbcType=JdbcType.VARCHAR),
        @Result(column="tool", property="tool", jdbcType=JdbcType.INTEGER),
        @Result(column="tool_fee", property="toolFee", jdbcType=JdbcType.VARCHAR),
        @Result(column="time_len", property="timeLen", jdbcType=JdbcType.INTEGER),
        @Result(column="leave_word", property="leaveWord", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })
    Offer selectByPrimaryKey(Integer id);


    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, offer_price, place_provided, place_fee, tool, ",
            "tool_fee, time_len, leave_word, status, created_time",
            "from offer",
            "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.INTEGER),
            @Result(column="offer_price", property="offerPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="place_provided", property="placeProvided", jdbcType=JdbcType.INTEGER),
            @Result(column="place_fee", property="placeFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="tool", property="tool", jdbcType=JdbcType.INTEGER),
            @Result(column="tool_fee", property="toolFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="time_len", property="timeLen", jdbcType=JdbcType.INTEGER),
            @Result(column="leave_word", property="leaveWord", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })
    Offer getOfferById(Integer id);


    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, offer_price, place_provided, place_fee, tool, ",
            "tool_fee, time_len, leave_word, status, created_time",
            "from offer",
            "where unit_id = #{unitId,jdbcType=INTEGER} and status != -1 and status != 1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.INTEGER),
            @Result(column="offer_price", property="offerPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="place_provided", property="placeProvided", jdbcType=JdbcType.INTEGER),
            @Result(column="place_fee", property="placeFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="tool", property="tool", jdbcType=JdbcType.INTEGER),
            @Result(column="tool_fee", property="toolFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="time_len", property="timeLen", jdbcType=JdbcType.INTEGER),
            @Result(column="leave_word", property="leaveWord", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Offer> selectByUnitId(Integer unitId);


    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, offer_price, place_provided, place_fee, tool, ",
            "tool_fee, time_len, leave_word, status, created_time",
            "from offer",
            "where unit_id = #{unitId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.INTEGER),
            @Result(column="offer_price", property="offerPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="place_provided", property="placeProvided", jdbcType=JdbcType.INTEGER),
            @Result(column="place_fee", property="placeFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="tool", property="tool", jdbcType=JdbcType.INTEGER),
            @Result(column="tool_fee", property="toolFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="time_len", property="timeLen", jdbcType=JdbcType.INTEGER),
            @Result(column="leave_word", property="leaveWord", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Offer> selectAllUnitByUnitId(Integer unitId);

    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, offer_price, place_provided, place_fee, tool, ",
            "tool_fee, time_len, leave_word, status, created_time",
            "from offer",
            "where actor_id = #{actorId,jdbcType=INTEGER} and status != 1"
    })
    List<Offer> selectReqOfferByActorId(int actorId);

    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, offer_price, place_provided, place_fee, tool, ",
            "tool_fee, time_len, leave_word, status, created_time",
            "from offer",
            "where actor_id = #{actorId,jdbcType=INTEGER} and unit_id = #{unitId,jdbcType=INTEGER} and status != -1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.INTEGER),
            @Result(column="offer_price", property="offerPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="place_provided", property="placeProvided", jdbcType=JdbcType.INTEGER),
            @Result(column="place_fee", property="placeFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="tool", property="tool", jdbcType=JdbcType.INTEGER),
            @Result(column="tool_fee", property="toolFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="time_len", property="timeLen", jdbcType=JdbcType.INTEGER),
            @Result(column="leave_word", property="leaveWord", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Offer> selectByActorIdAndUnitId(@Param("actorId") int actorId, @Param("unitId") int unitId);

    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, offer_price, place_provided, place_fee, tool, ",
            "tool_fee, time_len, leave_word, status, created_time",
            "from offer",
            "where actor_id = #{actorId,jdbcType=INTEGER} and status = 1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.INTEGER),
            @Result(column="offer_price", property="offerPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="place_provided", property="placeProvided", jdbcType=JdbcType.INTEGER),
            @Result(column="place_fee", property="placeFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="tool", property="tool", jdbcType=JdbcType.INTEGER),
            @Result(column="tool_fee", property="toolFee", jdbcType=JdbcType.VARCHAR),
            @Result(column="time_len", property="timeLen", jdbcType=JdbcType.INTEGER),
            @Result(column="leave_word", property="leaveWord", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Offer> selectAllPassedByActorId(int actorId);









    @Select({
            "select",
            "*",
            "from offer",
            "where unit_id = #{unitId,jdbcType=INTEGER}"
    })
    List<Offer> getStatisticByOfferId(int unitId);



//    @Select({
//            "select",
//            "count(actor_id)",
//            "from offer",
//            "where actor_id = #{actorId,jdbcType=INTEGER}"
//    })
//    int selectByActorId(int actorId);





    @UpdateProvider(type=OfferSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Offer record);

    @Update({
        "update offer",
        "set unit_id = #{unitId,jdbcType=INTEGER},",
          "owner_id = #{ownerId,jdbcType=INTEGER},",
          "actor_id = #{actorId,jdbcType=INTEGER},",
          "offer_price = #{offerPrice,jdbcType=INTEGER},",
          "place_provided = #{placeProvided,jdbcType=INTEGER},",
          "place_fee = #{placeFee,jdbcType=VARCHAR},",
          "tool = #{tool,jdbcType=INTEGER},",
          "tool_fee = #{toolFee,jdbcType=VARCHAR},",
          "time_len = #{timeLen,jdbcType=INTEGER},",
          "leave_word = #{leaveWord,jdbcType=VARCHAR},",
          "status = #{status,jdbcType=INTEGER},",
          "created_time = #{createdTime,jdbcType=TIMESTAMP}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Offer record);

    @Update({
            "update offer",
            "set status = #{status, jdbcType=INTEGER}",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int updateStatus(@Param("id") int id,@Param("status") int status);
}